/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeans;

import entity.Job;
import entity.Users;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import sessionBeans.RolesFacadeLocal;
import sessionBeans.UsersFacadeLocal;

/**
 *
 * @author Minh Luc
 */
@ManagedBean
@SessionScoped
public class EmployeeManagement implements Serializable {

    @EJB
    private RolesFacadeLocal rolesFacade;
    @EJB
    private UsersFacadeLocal usersFacade;
    private Users users = new Users();
    private Users selectUsers;
    private List<Users> list;
    private List<Users> listCode;
    private List<Users> filteredListCode;
    private List<Users> filteredList;
    private Job job = new Job();
    private SelectItem[] lockOption = new SelectItem[]{
        new SelectItem("", "All"), new SelectItem("True", "True"), new SelectItem("False", "False")
    };

    public void generateCode() {
        String code = UUID.randomUUID().toString();
        users.setCodeRegister(code);
        users.setEmail(code);
        users.setUserName(code);
    }

    public void create(ActionEvent event) {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        if (users.getCodeRegister() == null) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error users", "There is no Register Code");
        } else {
            try {
                if (System.Manager) {
                    getUsers().setRoles(rolesFacade.findByRoleName("Employee"));
                    getUsers().setJob(job);
                } else if (System.Admin) {
                    getUsers().setRoles(rolesFacade.findByRoleName("Manager"));
                }
                usersFacade.create(getUsers());
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Create users", "Create Successful");
            } catch (Exception e) {
                msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Create users Err", e.getMessage() + " Duplicate Name");
            }
        }

        setUsers(new Users());
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("create", true);
    }

    public void edit(ActionEvent event) {
        boolean frmDetailsEmployee = event.getComponent().getClientId().contains("frmDetailsEmployee");
        if (!frmDetailsEmployee) {
            if (!validate(selectUsers)) {
                return;
            }
        }

        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        if (System.Manager) {
            selectUsers.setJob(job);
        }
        try {
            usersFacade.edit(getSelectUsers());
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Edit users", "Edit Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Edit Error", e.getMessage());
        }

        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("edit", true);
    }

    public void copyCode() {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Clipboard clipboard = toolkit.getSystemClipboard();
        StringSelection selection = new StringSelection(selectUsers.getCodeRegister());
        clipboard.setContents(selection, null);
    }

    private List<Users> getEmployeeList() {
        if (System.user != null) {
            if (System.Admin) {
                return usersFacade.findByRoleName("Manager");
            } else if (System.Manager) {
                return usersFacade.findByRoleName("Employee");
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public Users getUsers() {
        return users;
    }

    public void setUsers(Users users) {
        this.users = users;
    }

    public Users getSelectUsers() {
        return selectUsers;
    }

    public void setSelectUsers(Users selectUsers) {
        this.selectUsers = selectUsers;
    }

    public List<Users> getList() {
        list = getEmployeeList();
        return list;
    }

    public void setList(List<Users> list) {
        list = getEmployeeList();
        this.list = list;
    }

    public List<Users> getFilteredList() {
        return filteredList;
    }

    public void setFilteredList(List<Users> filteredList) {
        this.filteredList = filteredList;
    }

    public Job getJob() {
        return job;
    }

    public void setJob(Job job) {
        this.job = job;
    }

    public SelectItem[] getLockOption() {
        return lockOption;
    }

    public void setLockOption(SelectItem[] lockOption) {
        this.lockOption = lockOption;
    }

    public List<Users> getListCode() {
        listCode = new ArrayList<Users>();
        if (System.user != null) {
            List<Users> listTemp = usersFacade.findAllCodeNotRegister();
            if (System.Admin) {
                for (int i = 0; i < listTemp.size(); i++) {
                    if (listTemp.get(i).getJob() == null) {
                        listCode.add(listTemp.get(i));
                    }
                }
            } else if (System.Manager) {
                for (int i = 0; i < listTemp.size(); i++) {
                    if (listTemp.get(i).getJob() != null) {
                        listCode.add(listTemp.get(i));
                    }
                }
            }
        }
        return listCode;
    }

    public void setListCode(List<Users> listCode) {
        this.listCode = listCode;
    }

    public List<Users> getFilteredListCode() {
        return filteredListCode;
    }

    public void setFilteredListCode(List<Users> filteredListCode) {
        this.filteredListCode = filteredListCode;
    }

    //Validate Employee
    public boolean validate(Users user) {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;

        if (user.getUserName() == null || user.getUserName().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Username is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (user.getUserName().length() < 2 || user.getUserName().length() > 16) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Username is from 2 - 16 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isContent(user.getUserName())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Username is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        if (user.getPassword() == null || user.getPassword().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Password is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (user.getPassword().length() < 2 || user.getPassword().length() > 16) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Password is from 2 to 16 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isContent(user.getPassword())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Password is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        if (user.getTitleOfCourtesy() == null || user.getTitleOfCourtesy().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "TitleOfCourtesy is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (user.getTitleOfCourtesy().length() < 2 || user.getTitleOfCourtesy().length() > 16) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "TitleOfCourtesy is greater 2 Characters and less than 16 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isContent(user.getTitleOfCourtesy())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "TitleOfCourtesy is only Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        if (user.getLastName() == null || user.getLastName().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Lastname is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (user.getLastName().length() < 2 || user.getLastName().length() > 16) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Lastname is from 2 to 16 Letters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isChar(user.getLastName())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Lastname is only Letter");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        if (user.getFristName() == null || user.getFristName().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Firstname is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (user.getFristName().length() < 2 || user.getFristName().length() > 16) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Firstname is from 2 - 16 Letters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isChar(user.getFristName())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Firstname is only Letter");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        if (user.getAddress() == null || user.getAddress().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Address is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (user.getAddress().length() < 2) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Address is greater 2 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isCharacter(user.getAddress())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Address is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        if (user.getCity() == null || user.getCity().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "City is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (user.getCity().length() < 2 || user.getCity().length() > 16) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "City is greater 2 Characters and less than 16 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isCharacter(user.getCity())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "City is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        if (user.getRegion() == null || user.getRegion().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Region is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (user.getRegion().length() < 2 || user.getRegion().length() > 16) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Region is greater 2 Characters and less than 16 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isCharacter(user.getRegion())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Region is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        if (user.getPostalCode() == null || user.getPostalCode().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "PostalCode is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (!(user.getPostalCode().length() == 5)) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "PostalCode is only 5 Numbers");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isInt(user.getPostalCode())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "PostalCode is only number");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        if (user.getMobilePhone() == null || user.getMobilePhone().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "MobilePhone is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (!util.Validate.isPhone(user.getMobilePhone())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "MobilePhone is from 8 - 14 numbers");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        if (user.getEmail() == null || user.getEmail().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Email is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (!util.Validate.isValidEmail(user.getEmail())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Format Email is xxx@xxx.xxx");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        if (user.getAnswer() == null || user.getAnswer().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Answer is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (user.getAnswer().length() < 2) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Answer is greater 2 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isCharacter(user.getAnswer())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "Answer is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        return true;
    }
}
